package com.chao.webflux.test;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MemoryConsumption {

    public static void main(String[] args) {
        int n = 10000000;
        Map<Integer, User> hashMap = new ConcurrentHashMap<>(n);
        for (int i = 0; i < n; i++) {
            User user = new User()
                    .setId(i)
                    .setName("fwc-" + i);
            hashMap.put(i,user);
        }
        // 计算 HashMap 占用的内存
        long start = Runtime.getRuntime().freeMemory();
        System.gc(); // 调用垃圾回收
        long end = Runtime.getRuntime().freeMemory();
        long occupy = end - start;
        System.out.println("HashMap 占用的内存: " + (occupy/(1024*1024)) + " MB");
    }

    @Data
    @Accessors(chain = true)
    private static class User {

        private int id;

        private String name;
    }
}
